﻿using RimWorld;
using RimWorld.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace SZ.Mech
{
    /// <summary>
    /// 跳跃加爆炸
    /// </summary>
    public class Verb_JumpExplosion : Verb
    {
        private float cachedEffectiveRange = -1f;

        public override float EffectiveRange
        {
            get
            {
                if (cachedEffectiveRange < 0f)//准备跳远的范围
                {
                    cachedEffectiveRange = verbProps.range;
                }
                return cachedEffectiveRange;
            }
        }

        public override bool MultiSelect => true;

        protected override bool TryCastShot()
        {
           return JumpExplosionUtility.DoJump(CasterPawn, currentTarget, base.ReloadableCompSource, verbProps);
          
            //return JumpUtility.DoJump(CasterPawn, currentTarget, base.ReloadableCompSource, verbProps);
        }

        public override void OrderForceTarget(LocalTargetInfo target)
        {
            JumpUtility.OrderJump(CasterPawn, target, this, EffectiveRange);
        }

        public override bool ValidateTarget(LocalTargetInfo target, bool showMessages = true)
        {

            if (caster == null)
            {
                return false;
            }
            //JumpComp jumpComp = caster.TryGetComp<JumpComp>();
            //if (jumpComp.cdTick < 0)
            //{
            //    jumpComp.cdTick = jumpComp.Props.CD;
            //}
            //else
            //{
            //    return false;
            //}
        
            if (!CanHitTarget(target) || !JumpUtility.ValidJumpTarget(caster.Map, target.Cell))
            {
                return false;
            }
            if (!ReloadableUtility.CanUseConsideringQueuedJobs(CasterPawn, base.EquipmentSource))
            {
                return false;
            }
            return true;
        }

        public override bool CanHitTargetFrom(IntVec3 root, LocalTargetInfo targ)
        {
            return JumpUtility.CanHitTargetFrom(CasterPawn, root, targ, EffectiveRange);
        }

        public override void OnGUI(LocalTargetInfo target)
        {
            if (CanHitTarget(target) && JumpUtility.ValidJumpTarget(caster.Map, target.Cell))
            {
                base.OnGUI(target);
            }
            else
            {
                GenUI.DrawMouseAttachment(TexCommand.CannotShoot);
            }
        }

        public override void DrawHighlight(LocalTargetInfo target)
        {
            if (target.IsValid && JumpUtility.ValidJumpTarget(caster.Map, target.Cell))
            {
                GenDraw.DrawTargetHighlightWithLayer(target.CenterVector3, AltitudeLayer.MetaOverlays);
            }
            GenDraw.DrawRadiusRing(caster.Position, EffectiveRange, Color.white, (IntVec3 c) => GenSight.LineOfSight(caster.Position, c, caster.Map) && JumpUtility.ValidJumpTarget(caster.Map, c));
        }
    }
}
